class Solution
{
public:
    long long matrixSumQueries(int n, vector<vector<int>> &queries)
    {
        long long result = 0;
        unordered_set<int> occupiedRows;
        unordered_set<int> occupiedCols;
        for (int i = queries.size() - 1; i >= 0; --i)
        {
            int type = queries[i][0];
            int index = queries[i][1];
            int value = queries[i][2];

            if (type == 0)
            {
                if (!occupiedRows.count(index))
                {
                    result += value * (n - occupiedCols.size());
                    occupiedRows.insert(index);
                }
            }
            else
            {
                if (!occupiedCols.count(index))
                {
                    result += value * (n - occupiedRows.size());
                    occupiedCols.insert(index);
                }
            }
        }
        return result;
    }
};